﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.ServiceModel.Activation;
using UFIDA.U9.WSS.ProxyService.Exceptions;
using UFIDA.U9.WSS.ProxyService.Interfaces;
using UFIDA.U9.WSS.ProxyService.Models;
using UFIDA.U9.WSS.ProxyService.Utils;
using UFSoft.UBF.Service.Base;

namespace UFIDA.U9.WSS.ProxyService.Services
{
    /// <summary>
    ///     代理服务
    /// </summary>
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ProxyService : IProxyService
    {
        /// <summary>
        ///     执行代理请求
        /// </summary>
        /// <param name="proxyBase"></param>
        /// <returns></returns>
        [Description("通用代理方法")]
        public ReturnMessage<object> Do(ProxyBase proxyBase)
        {
            if (proxyBase == null)
                throw new WSException(ExceptionCodes.ProxyServiceProxyTypePropertyIsIncorrect, "proxy is incorrect");
            MethodInfo methodInfo = proxyBase.GetType().GetMethod(Constant.ProxyDoMethodName, new Type[] { });
            if (methodInfo == null)
                throw new WSException(ExceptionCodes.ProxyServiceProxyTypePropertyIsIncorrect,
                    "no find Do() method in proxy");
            try
            {
                //禁止检查下一个代理有效
                SessionObject.DisableCheckOneProxyEnabled();
                //反射调用
                object result = methodInfo.Invoke(proxyBase, null);
                return new ReturnMessage<object>(result);
            }
            catch (Exception ex)
            {
                throw new WSException(ExceptionCodes.ProxyServiceCallFail, string.Empty, ex);
            }
        }
    }
}